package com.hwl.demo5.context;

import com.hwl.demo3.service.ISale;
import com.hwl.demo5.service.IShopFactory;
import com.hwl.demo5.service.impl.CashRebateReturnFactory;
import com.hwl.demo5.service.impl.CashReturnRebateFactory;

/**
 * 简单工厂+策略+装饰+工厂方法
 *
 * @author sentry
 * @since 2023-03-18
 */
public class CashContext {
    private final ISale cs;

    public CashContext(int cashType) {
        IShopFactory fs;
        switch (cashType) {
            case 1:
                // 原价
                fs = new CashRebateReturnFactory(1d, 0d, 0d);
                break;
            case 2:
                // 打8折
                fs = new CashRebateReturnFactory(0.8d, 0d, 0d);
                break;
            case 3:
                // 打7折
                fs = new CashRebateReturnFactory(0.7d, 0d, 0d);
                break;
            case 4:
                // 满300减100
                fs = new CashReturnRebateFactory(1d, 300d, 100d);
                break;
            case 5:
                // 先打8折，再满300减100
                fs = new CashRebateReturnFactory(0.8d, 300d, 100d);
                break;
            case 6:
                // 先满200减50，再打7折
                fs = new CashReturnRebateFactory(0.7d, 200d, 50d);
                break;
            default:
                throw new RuntimeException("暂不支持优惠类型");
        }
        this.cs = fs.createSalesMode();
    }

    public static void main(String[] args) {
        CashContext cc = new CashContext(5);
        double result = cc.getResult(150, 3);
        System.out.println("价格为：" + result);
    }

    public double getResult(double price, int num) {
        return this.cs.acceptCash(price, num);
    }
}
